package com.ychs.servlet;

import com.ychs.entity.User;
import com.ychs.service.IBaseService;
import com.ychs.service.UserService;
import com.ychs.util.Constants;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * copyright(c)2021 YYB.ALL rights Reserved
 * <p>
 * 描述:
 *
 * @author 原玉波
 * @version 1.0
 * @date 2021/7/13
 */
@WebServlet(value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private Logger logger = Logger.getLogger(this.getClass());
    private IBaseService<User> service = new UserService();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        logger.info("enter login method");
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        Map<String, Object> conditions = new HashMap<>();

        if (username!=null&&password!=null){
            conditions.put("username", username);
            conditions.put("password", password);
        }

        //标识登录是否成功
        boolean loginFlag = false;


        int count = service.searchTotalNumber(conditions);

        if(count==1){
            loginFlag = true;
        }

        if(loginFlag){
            //登录成功
            User user  = new User();
            //session 中放入当前登录的用户，登录过滤器中要判断
            request.getSession().setAttribute("user",user);
            response.sendRedirect("index.jsp");
        }else{
            response.sendRedirect("login.jsp");
        }
    }

}
